﻿using System;

using HsTec.Mobile.Green.Util;
using HsTec.Mobile.Green.BLL;
using Baimasoft.Manage.SqlServerDAL;

public partial class DataStorage_DataImport : System.Web.UI.Page
{
    DataStorageBLL bll = new DataStorageBLL();
    Param MyP = new Param();

    protected void Page_Load(object sender, EventArgs e)
    {
        sltType.Attributes["onchange"] = "sltChange(this)";
        if (!IsPostBack)
        {
            BindsltArea();
            txtYearsDataBind();
        }
    }
    protected void btnImport_ServerClick(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(txtYears.Text.Trim()))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择导入年月！');</script>");
            return;
        }
        if (String.IsNullOrEmpty(sltType.SelectedValue.Trim()))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择导入数据类型！');</script>");
            return;
        }
        string sUpLoadDate = Util.FormateData(txtYears.Text.Trim());
        if (sltType.SelectedIndex == 5 || sltType.SelectedIndex == 6)
        {
            if (String.IsNullOrEmpty(sltArea.Value.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择导入地区！');</script>");
                return;
            }
            //1.验证是否是通过下划线分割的文件名
            string[] sFileName = fuDataTable.FileName.Split('_');
            if (sFileName.Length != 3)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('入库的文件名不符要求！（如：节能减排规划指标表_" + sltArea.Items[sltArea.SelectedIndex].Text.Trim() + "_" + sUpLoadDate + ".xls）');</script>");
                return;
            }
            //2.验证上传文件名
            if (!ValidateHelper.ValidateFileName(fuDataTable.FileName, "节能减排规划指标表", sltArea.Items[sltArea.SelectedIndex].Text.Trim(), sUpLoadDate))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('入库的文件名不符要求！（如：节能减排规划指标表_" + sltArea.Items[sltArea.SelectedIndex].Text.Trim() + "_" + sUpLoadDate + ".xls）');</script>");
                return;
            }
        }
        else
        {
            //1.验证是否是通过下划线分割的文件名
            string[] sFileName = fuDataTable.FileName.Split('_');
            if (sFileName.Length != 2)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('入库的文件名不符要求！（如：" + sltType.Items[sltType.SelectedIndex].Text.Trim() + "_" + sUpLoadDate + ".xls）');</script>");
                return;
            }
            //2.验证上传文件名
            if (!ValidateHelper.ValidateFileName(fuDataTable.FileName, sltType.Items[sltType.SelectedIndex].Text.Trim(), sUpLoadDate))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('入库的文件名不符要求！（如：" + sltType.Items[sltType.SelectedIndex].Text.Trim() + "_" + sUpLoadDate + ".xls）');</script>");
                return;
            }
        }
        //3.验证上传文件类型
        string UpfileExt = System.IO.Path.GetExtension(fuDataTable.FileName);
        if (UpfileExt != ".xls")
        {
            Text_Confirm_Upstate.Text = "入库的文件名类型不符要求！（必须是EXCEL文件）";
            return;
        }
        //4.验证数据是否已经上传
        //4.1 上传文件
        int sType = sltType.SelectedIndex + 1;
        switch (Util.Upload_File(fuDataTable, fuDataTable.FileName, "0" + sType))
        {
            case 1:
                Text_Confirm_Upstate.Text = "上传文件成功！";
                break;
            case 0:
                Text_Confirm_Upstate.Text = "上传文件失败！";
                return;
        }
        //4.2 导入到DataSet中
        ExcelHelper ehelper = new ExcelHelper();
        string[] ary = sltType.SelectedValue.Split('|');
        ehelper.insertTableName = ary[0];
        ehelper.excelPath = "0" + sType + "\\" + fuDataTable.FileName;
        ehelper.sheetName = String.Empty;
        //----------------------------------------------需要修改-------------------------------------//
        //Text_Confirm_Upstate.Text = ehelper.importImportReport(sltArea.Value.Trim(), sUpLoadDate, Util.FormateData(Session["Baimasoft_Pers_ID"].ToString()));
        switch (sltType.SelectedIndex)
        {
            case 1:
                Text_Confirm_Upstate.Text = ehelper.importExcel(sUpLoadDate, Session["Baimasoft_Pers_ID"].ToString());
                break;
            case 2:
                Text_Confirm_Upstate.Text = ehelper.importTDExcel(sUpLoadDate, Session["Baimasoft_Pers_ID"].ToString());
                break;
            case 3:
                Text_Confirm_Upstate.Text = ehelper.importExcel(sUpLoadDate, Session["Baimasoft_Pers_ID"].ToString());
                break;
            case 4:
                Text_Confirm_Upstate.Text = ehelper.importExcel(sUpLoadDate, Session["Baimasoft_Pers_ID"].ToString());
                break;
            case 5:
                Text_Confirm_Upstate.Text = ehelper.importESExcel(sltArea.Value.Trim(), Util.FormateData(txtYears.Text.Trim()), Session["Baimasoft_Pers_ID"].ToString());
                break;
            case 6:
                Text_Confirm_Upstate.Text = ehelper.importES09Excel(sltArea.Value.Trim(), Util.FormateData(txtYears.Text.Trim()), Session["Baimasoft_Pers_ID"].ToString());
                break;
        }
        Util.Delete_File(fuDataTable.FileName, "0" + sType);
    }
    private void BindsltArea()
    {
        sltArea.DataTextField = "Name";
        sltArea.DataValueField = "ID";
        sltArea.DataSource = bll.GetAreaList();
        sltArea.DataBind();
        sltArea.Items.Insert(0, "请选择县市区域...");
        sltArea.Items[0].Value = "";
    }

    private void txtYearsDataBind()
    {
        txtYears.DataTextField = "Name";
        txtYears.DataValueField = "Name";
        txtYears.DataSource = MyP.GetCodeByParentCode("1");
        txtYears.DataBind();
        txtYears.Items.Insert(0, "请选择...");
        txtYears.Items[0].Value = "";
    }
}
